package com.pkk.penguin.frame.know.java.Javacoreknowledge.thread.callable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @description: callabll的线程执行器
 * @author: peikunkun
 * @create: 2019-12-23 18:15
 **/
public class CallableRunnner {

  public static void main(String[] args) throws ExecutionException, InterruptedException {

    //池子最大线程数
    int MAX_IMUM_POOL_SIZE = 10;
    TimeUnit unit = TimeUnit.MILLISECONDS;

    //创建一个线程池
    ExecutorService executorService = Executors.newFixedThreadPool(MAX_IMUM_POOL_SIZE);

    List<Future> futures = new ArrayList<>();
    for (int i = 0; i < MAX_IMUM_POOL_SIZE; i++) {
      final CallableThread callableThread = new CallableThread("第几个线程:" + i);
      // 执行任务并获取 Future 对象
      final Future<String> submit = executorService.submit(callableThread);
      futures.add(submit);
    }
    //停止接收新任务，原来的任务继续执行
    executorService.shutdown();

    //带返回值的信息获取
    for (Future f : futures) {
      System.out.println(f.get().toString());
    }
  }
}
